<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="listQuery.keywords" placeholder="请输入搜索内容" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">搜索</el-button>
      <el-button v-waves class="filter-item" type="primary" @click="addRole">新增</el-button>
    </div>

    <!--表格开始-->
    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column label="ID" width="120px" align="center">
        <template slot-scope="scope">
          <el-tag>{{ scope.row.id }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="添加时间" width="250px" align="center">
        <template slot-scope="scope">
          <el-tag>{{ renderTime(scope.row.createdAt) }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="角色" width="220px" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.remake }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" min-width="100px" align="center">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="edit(scope.row)">编辑</el-button>
          <el-button type="danger" size="mini" @click="delRoles(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
    <el-dialog
      title="编辑"
      :visible.sync="editDialog"
      width="40%"
      :before-close="editClose"
    >
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="菜单">
          <el-checkbox-group v-model="form.menus">
            <el-checkbox v-for="item in menus" :key="item.id" :label="item.id">{{ item.name }}</el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">保存</el-button>
          <el-button @click="editDialog = false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
    <el-dialog
      title="新增"
      :visible.sync="roles"
      width="40%"
      :before-close="editClose"
    >
      <el-form ref="form" :model="res" label-width="80px">
        <el-form-item label="角色名">
          <el-input v-model="res.role" />
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="res.remake" />
        </el-form-item>
        <el-form-item label="菜单">
          <el-checkbox-group v-model="res.menus">
            <el-checkbox v-for="item in menus" :key="item.id" :label="item.id">{{ item.name }}</el-checkbox>
          </el-checkbox-group>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onAdd">保存</el-button>
          <el-button @click="roles = false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import waves from '@/directive/waves'
import Pagination from '@/components/Pagination'
import { roles, editRole, addRoles, delRole } from '@/api/roles'
import { menu } from '@/api/menu'
import { renderTime } from '../../utils'
export default {
  name: 'Admins',
  components: { Pagination },
  directives: { waves },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      collection: '',
      listLoading: false,
      listQuery: {
        page: 1,
        limit: 10,
        keywords: undefined
      },
      form: {
        id: undefined,
        menus: []
      },
      res: {
        role: '',
        remake: '',
        menus: []
      },
      menus: [],
      editDialog: false,
      roles: false
    }
  },
  created() {
    this.getList()
    this.getMenu()
  },
  methods: {
    renderTime,
    // 获取数据
    getList() {
      this.listLoading = true
      roles(this.listQuery).then(res => {
        const { data: { count, rows }} = res.data
        this.total = count
        this.list = rows
        setTimeout(() => {
          this.listLoading = false
        }, 500)
      }).catch(err => {
        console.log(err)
      })
    },
    // 分页
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    // 编辑
    edit(row) {
      if (row.id === 1) {
        this.$message({
          type: 'error',
          message: '不能操作超级管理员'
        })
        return false
      }
      const menu = row.menuMap
      const res = []
      menu.forEach(item => {
        res.push(parseInt(item))
      })
      this.form.id = row.id
      this.form.menus = res
      this.editDialog = true
    },
    // 删除
    delRoles(id) {
      if (id === 1) {
        this.$message({
          type: 'error',
          message: '不能删除超级管理员角色'
        })
        return false
      }
      this.$confirm('此操作将删除该角色, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delRole(id)
          .then(res => {
            const { msg } = res.data
            this.$message({
              type: 'success',
              message: msg
            })
            this.getList()
          })
          .catch(err => {
            console.log(err)
          })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消'
        })
      })
    },
    // addRole
    addRole() {
      this.roles = true
    },
    // editClose
    editClose() {
      this.roles = false
      this.editDialog = false
    },
    //  onSubmit
    onSubmit() {
      const data = { id: this.form.id, menus: this.form.menus }
      if (data.menus.length <= 0) {
        this.$message({
          type: 'error',
          message: '请选择，不能为空'
        })
        return false
      }
      editRole(data).then(res => {
        const { msg } = res.data
        this.$message({
          message: msg,
          type: 'success'
        })
        this.editDialog = false
        this.getList()
      }).catch(e => {
        console.log(e)
      })
    },
    // onAdd
    onAdd() {
      const data = { role: this.res.role, remake: this.res.remake, menus: this.res.menus }
      if (data.menus.length <= 0) {
        this.$message({
          type: 'error',
          message: '请选择，不能为空'
        })
        return false
      }
      addRoles(data).then(res => {
        const { msg } = res.data
        this.$message({
          message: msg,
          type: 'success'
        })
        this.roles = false
        this.getList()
      }).catch(e => {
        console.log(e)
      })
    },
    // getMenu
    getMenu() {
      menu().then(res => {
        const { data } = res.data
        this.menus = data
      }).catch(e => {
        console.log(e)
      })
    }
  }
}
</script>
<style></style>
